﻿#region 程序集引用
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述： 中文分词 (Chinese Word Segmentation) 
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        /// <summary>
        /// 中文分词 (Chinese Word Segmentation) 
        /// 对传入的字符串进行分词
        /// </summary>
        public static class CWS
        {
           /// <summary>
            /// 对传入的字符串进行分词
           /// </summary>
           /// <param name="word">待分词文本</param>
           /// <returns>分词结果，一个词数组</returns>
            public static string[] Execute(string word)
            {
                string[] arrSegment;

                arrSegment = new string[1] { word };
               
                return arrSegment;
            }


            private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
            private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
            private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

            [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);

            /// <summary>
            /// 将字符转换成简体中文
            /// </summary>
            /// <param name="source">输入要转换的字符串</param>
            /// <returns>转换完成后的字符串</returns>
            public static string ToSimplified(string source)
            {
                String target = new String(' ', source.Length);
                int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);
                return target;
            }

            /// <summary>
            /// 将字符转换为繁体中文
            /// </summary>
            /// <param name="source">输入要转换的字符串</param>
            /// <returns>转换完成后的字符串</returns>
            public static string ToTraditional(string source)
            {
                String target = new String(' ', source.Length);
                int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
                return target;
            }
        }
    }
}
